﻿export default {
  props: ['formItemConfig'],
  data () {
    return {
      dimensionList: [], // 维度
      measurementList: [], // 量度
      dataList: [], // 数据集的所有数据
      setList: [] // 数据集列表
    }
  },
  created () {
    this.getAllData()
  },
  methods: {
    // 获取所有启用的数据集列表
    getAllData () {
      this.dimensionList = []
      this.measurementList = []
      this.setList = []
      this.dataList = []
      this.$get('/data_set/activelist').then((data) => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.setList = dataResult.data
          this.currentSetChange()
        } else {
          this.$message({
            message: dataResult.msg,
            type: 'warning'
          })
        }
      }).catch((error) => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    // 指定的数据集改变时
    currentSetChange () {
      this.dimensionList = []
      this.measurementList = []
      if (!this.formItemConfig.dataSet) {
        return
      }
      this.excuteCurrentSet()
      this.getXYZofCurrentSet()
    },
    // 执行指定的数据集
    excuteCurrentSet () {
      this.dataList = []
      this.$post('/data_set/' + this.formItemConfig.dataSet + '/excute', {
        limit: 10000,
        page: 1
      }).then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.dataList = dataResult.data.data.QueryDatas
          if (this.formItemConfig.type === 'table') {
            this.formItemConfig.tableData = dataResult.data.data.QueryDatas
            this.formItemConfig.tableColumn = []
            if (this.formItemConfig.tableData.length) {
              let tableDataObj = this.formItemConfig.tableData[0]
              for (let key in tableDataObj) {
                let obj = {
                  text: key,
                  indexName: key,
                  isShow: true,
                  sortNum: this.formItemConfig.tableColumn.length
                }
                this.formItemConfig.tableColumn.push(obj)
              }
            }
          }
        }
      })
    },
    // 获取指定数据集的维度和量度
    getXYZofCurrentSet () {
      this.$get('/data_set/' + this.formItemConfig.dataSet + '/xyz').then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.dimensionList = dataResult.data.dimension
          this.measurementList = dataResult.data.measurement
        }
      })
    },
    // 维度改变时
    dimensionChange () {
      if (this.formItemConfig.type == 'echarts_bar' || this.formItemConfig.type == 'echarts_line') {
        this.formItemConfig.dataOptions.xAxis.data = []
        if (this.formItemConfig.type === 'echarts_bar') { // 柱状图时
          this.formItemConfig.dataOptions.yAxis.data = []
        }
        if (this.formItemConfig.dimension) {
          this.dataList.forEach(item => {
            this.formItemConfig.dataOptions.xAxis.data.push(item[this.formItemConfig.dimension])
            if (this.formItemConfig.type === 'echarts_bar') { // 柱状图时
              this.formItemConfig.dataOptions.yAxis.data.push(item[this.formItemConfig.dimension])
            }
          })
        }
      } else if (this.formItemConfig.type === 'echarts_pie'){ // 饼图时
        this.measurementChange_pie()
      }
    },
    // 量度改变时
    measurementChange () {
      this.formItemConfig.dataOptions.legend.data = []
      this.formItemConfig.dataOptions.series = []
      if (this.formItemConfig.measurement && this.formItemConfig.measurement.length) {
        for (let i = 0; i < this.formItemConfig.measurement.length; i++) {
          this.formItemConfig.dataOptions.legend.data.push(this.formItemConfig.measurement[i])
          let obj = {
            name: this.formItemConfig.measurement[i],
            type: this.formItemConfig.options.type,
            itemStyle: { // 线条的验颜色
              color: ''
            },
            label: {
              show: false,
              distance: 5, // 距离元素的距离
              rotate: 0, // 旋转角度 -90 ~90
              formatter: '{c}',
              color: '',
              fontSize: 12,
              position: 'top' // top, bottom, left,right,inside,insideLeft,insideRight,insideTop,insideBottom
            },
            data: []
          }
          if (this.formItemConfig.type === 'echarts_line') { // 折线图
            obj['symbol'] = 'emptyCircle'
            obj['symbolSize'] = 4
            obj['areaStyle'] = {
              color: '',
              opacity: 0
            }
          } else if (this.formItemConfig.type === 'echarts_bar') { // 柱状图
            obj['barWidth'] = '' // 柱条的宽度
            obj['stack'] = '' // 数据堆叠(相同名称会堆叠)
            obj['barGap'] = '30%' // 不同系列的柱间距离
            obj['barCategoryGap'] = '20%' // 同一系列的柱间距离
          }
          this.dataList.forEach(item => {
            obj.data.push(item[this.formItemConfig.measurement[i]])
          })
          this.formItemConfig.dataOptions.series.push(obj)
        }
      }
    },
    // 量度改变时---饼图
    measurementChange_pie () {
      this.formItemConfig.dataOptions.legend.data = []
      this.formItemConfig.dataOptions.series = [
        {
          name: '',
          type: 'pie',
          radius: ['0', '70%'],
          center: ['50%', '50%'], // 饼图的中心
          roseType: false, // 南丁格尔图(false,radius,area)
          label: {
            show: true,
            position: 'outside', // outside, inside, inner,center
            formatter: '{b}:{c}({d}%)', // {a}系列名,{b}数据名{c}数据值{d}百分比 \n换行
            color: '#333',
            fontSize: 12
          },
          labelLine: {
            show: true,
            lineStyle: {
              color: '#333'
            }
          },
          data: []
        }
      ]
      if (this.formItemConfig.measurement && this.formItemConfig.dimension) {
        this.dataList.forEach(item => {
          // legend的值是维度
          this.formItemConfig.dataOptions.legend.data.push(item[this.formItemConfig.dimension])
          let obj = {
            value: item[this.formItemConfig.measurement], // 值为量度
            name: item[this.formItemConfig.dimension],
            itemStyle: { // 图例样式
              color: ''
            }
          }
          this.formItemConfig.dataOptions.series[0].data.push(obj)
        })
      }
    }
  },
  watch: {
    // 选中的元素变化时
    'formItemConfig.model': {
      handler: function (val, oldVal) {
        this.getAllData()
      },
      deep: true
    }
  }
}
